<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-item label="任务发起人">
              <j-select-user-by-dep :multi="false" v-model="queryParam.creator"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24" >
            <a-form-item label="流程编码">
              <a-input v-model="queryParam.modelKey" placeholder="请输入流程编码"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24" >
            <a-form-item label="流程名称">
              <a-input v-model="queryParam.modelName" placeholder="请输入流程名称"/>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="4" :md="4" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button  type="primary" icon="search" @click="searchQuery">查询</a-button>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        size="middle"
        bordered
        row-key="id"
        :columns="columns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
        :scroll="{x:1200}"
      >
        <template slot="action" slot-scope="text, record">
          <a @click="historyClick(record)">历史</a>
          <a-divider type="vertical" v-if="record.status === 1 || record.multi === 1" />
          <a-dropdown v-if="record.status === 1 || record.multi === 1">
            <a class="ant-dropdown-link">更多<a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
               <a @click="handleEntrustClick(record)">委托</a>
              </a-menu-item>
              <!-- <a-menu-item>
                <a>跳转</a>
              </a-menu-item> -->
              <a-menu-item>
                <a-popconfirm title="确定作废吗?" @confirm="() =>  handleAbolishTask(record)">
                  <a>作废</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </a-table>
    </div>
    <!-- 任务办理弹窗 -->
    <task-modal ref="taskModal" :taskData="taskData"  type="history_his" title="流程历史"></task-modal>
    <!-- 任务委托弹窗 -->
    <task-entrust-modal ref="taskEntrustModal" @ok="() => this.loadData()"></task-entrust-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
import taskModal from './modal/TaskModal'
import { cancellation } from '@/api/task'
// import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import taskEntrustModal from './modal/taskEntrustModal'
import STable from '@/components/table'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'ProcessInstanceList',
  components: {
    JSelectUserByDep,
    taskModal,
    // JSelectMultiUser,
    taskEntrustModal,
    STable
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '流程名称',
          align: 'center',
          dataIndex: 'modelName',
          width: 100
        },
        {
          title: '流程编码',
          align: 'center',
          dataIndex: 'modelKey',
          width: 100
        },
        {
          title: '业务标题',
          align: 'center',
          dataIndex: 'instanceName'
        },
        {
          title: '流程实例',
          align: 'center',
          dataIndex: 'processInstanceId'
        },
        {
          title: '发起人',
          align: 'center',
          dataIndex: 'creator',
          width: 100
        },
        {
          title: '办理人',
          align: 'center',
          dataIndex: 'assigneeName',
          width: 100
        },
        {
          title: '流程状态',
          align: 'center',
          dataIndex: 'status',
          width: 100,
          customRender: (text) => {
            return filterDictTextByCache('bpmn_status', text)
          }
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'startTime'
        },
        {
          title: '耗时',
          align: 'center',
          dataIndex: 'totalTime',
          width: 120
        },
        {
          title: '当前环节',
          align: 'center',
          dataIndex: 'taskName',
          width: 100
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 150,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/activiti/task/getAllTask'
      },
      taskData: {},
      form: this.$form.createForm(this),
      taskTitle: '',
      excludeKeys: ['modelKey', 'modelName', 'creator']
    }
  },
  methods: {
    handleAbolishTask(record) {
      cancellation({ procInstId: record.processInstanceId }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success('流程已作废')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    historyClick(record) { // 点击历史
      this.taskData = record
      this.$refs.taskModal.editModalVisible = true
    },
    // handleClick(data) {
    //   this.taskData = data
    //   this.taskTitle = '【' + data.modelName + '】流程办理'
    //   this.$refs.taskModal.editModalVisible = true
    // },
    // completeTaskSubmit(data, appendFormData) { // 流程审批
    //   completeTask({
    //     comment: data.reason,
    //     taskId: this.taskData.id,
    //     procInstId: this.taskData.processInstanceId,
    //     formDataId: this.taskData.formDataId,
    //     usernames: data.ccUserRealNames,
    //     flag: data.flag,
    //     appendFormId: this.taskData.appendForm && this.taskData.appendForm.id,
    //     formData: this.taskData.appendForm && JSON.stringify(appendFormData),
    //     appendFormDataId: data.appendFormDataId,
    //     nodeId: data.nodeId,
    //     files: data.files
    //   }).then(res => {
    //     if (res.success) {
    //       this.$message.success('流程办理成功')
    //       this.loadData()
    //       this.$refs.taskModal.editModalVisible = false
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   }).catch((error) => {
    //     console.log(error)
    //   })
    // },
    handleEntrustClick(data) { // 点击委托
      this.$refs.taskEntrustModal.show(data.taskId)
    }
    // taskEntrust(data) {
    //   const that = this
    //   if (!data) {
    //     this.$message.error('请先选择委托人')
    //     return false
    //   }
    //   entrustTask({ taskId: this.taskData.taskId, userId: data }).then(function(res) {
    //     if (res.success) {
    //       that.$message.success('流程委托成功')
    //       that.loadData()
    //       that.$refs.taskEntrustModal.modalVisible = false
    //     } else {
    //       that.$message.error(res.message)
    //     }
    //   })
    // }
  }
}
</script>
